package com.odais.mvc.seller;

import android.content.Context;
import android.opengl.Visibility;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.odais.R;
import com.odais.DataManagement.DataManager;
import com.odais.DataManagement.ReservationRepository;
import com.odais.Reservation.Reservation;
import com.odais.Reservation.ReservationList;

/**
 *
 * @author Lionel DECELLE
 * @since 8 juin 2012 - 10:53:53
 *
 */
public class sellerListViewPendingLVAdapter extends ArrayAdapter<Reservation> {

	        private ReservationList items;
	        private Context mContext;
	        private DataManager dataManager;
	    	private ReservationRepository reservationRepository;

	        public sellerListViewPendingLVAdapter(Context context, DataManager dataManager, int textViewResourceId, ReservationList reservationList) {
	                super(context, textViewResourceId);
	                this.items = reservationList;
	                mContext = context;
	                this.dataManager = dataManager;

	    	    	reservationRepository = new ReservationRepository(mContext);
	        }
	        
	        public void Update(ReservationList reservationList) {
                this.items = reservationList;
	        }
	        
	        public void Update() {
	        	//this.notifyDataSetInvalidated();
	        	//this.notifyDataSetChanged();
	        	dataManager.UpdateReservationList(DataManager.UPDATE_TYPE_PENDING_LIST);
	        	dataManager.UpdateReservationList(DataManager.UPDATE_TYPE_CONFIRMED_LIST);
	        }

	        @Override
	        public View getView(int position, View convertView, ViewGroup parent) {
	                View v = convertView;
	                if (v == null) {
	                    LayoutInflater vi = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	                    v = vi.inflate(R.layout.sellerlistviewpending_list, null);
	                }
	                Reservation o = items.get(position);
	                if (o != null) {
	                		/*
	                		 * Displays info like this :
	                		 * Name : Name
	                		 * Date : Date - Hour
	                		 * People : Number of people
	                		 * Table : Table number
	                		 * Status : Status	                	
	                		*/
	                		TextView tv_name = (TextView) v.findViewById(R.id.tvsellerlistpending_name);
	                		TextView tv_date = (TextView) v.findViewById(R.id.tvsellerlistpending_date);
	                		TextView tv_npeople = (TextView) v.findViewById(R.id.tvsellerlistpending_npeople);
	                		TextView tv_table = (TextView) v.findViewById(R.id.tvsellerlistpending_table);
	                        TextView tv_status = (TextView) v.findViewById(R.id.tvsellerlistpending_status);
	                        Button bt_accept = (Button) v.findViewById(R.id.btsellerlistpending_accept);
	                        Button bt_decline = (Button) v.findViewById(R.id.btsellerlistpending_decline);

	                        if (tv_name != null) {
	                        	tv_name.setText(mContext.getString(R.string.tvsellerlist_name) + " " + o.getClientName());
	                        }
	                        if (tv_date != null) {
	                        	tv_date.setText(mContext.getString(R.string.tvsellerlist_date) + " " + o.getItemTimestampRsv());
	                        }
	                        if (tv_npeople != null) {
	                        	tv_npeople.setText(mContext.getString(R.string.tvsellerlist_npeople) + " " + String.valueOf(o.getItemNPeople()));
	                        }
	                        if (tv_table != null) {
	                        	tv_table.setText(mContext.getString(R.string.tvsellerlist_table) + " " + String.valueOf(o.getItemNumTable()));
	                        }
	                        if (tv_status != null) {
	                        	tv_status.setText(mContext.getString(R.string.tvsellerlist_status) + " " + String.valueOf(o.getItemStatus()));
	                        }
	                        
	                        if((bt_accept != null) && (o.getItemStatus()==DataManager.RESERVATION_STATUS_PENDING)){
	                        	bt_accept.setText("Accept");
	                        	bt_accept.setVisibility(Button.VISIBLE);
		                        bt_accept.setOnClickListener(new AcceptListener(mContext, position));
	                        }
	                        else
	                        {
	                        	bt_accept.setText("Accept");
	                        	bt_accept.setVisibility(Button.GONE);
	                        }
	                        
	                        if((bt_decline != null) && (o.getItemStatus()==DataManager.RESERVATION_STATUS_CONFIRMED)){
	                        	bt_decline.setText("Decline");
	                        	bt_decline.setVisibility(Button.VISIBLE);
		                        bt_decline.setOnClickListener(new DeclineListener(mContext, position));
	                        }
	                        else
	                        {
	                        	bt_decline.setText("Decline");
	                        	bt_decline.setVisibility(Button.GONE);
	                        }

	                }
	                return v;
	        }



	        //**********************************************
	        //	Listeners
	        //**********************************************
	        class AcceptListener implements View.OnClickListener{
	        	private Context context;
	        	private int position;

	    		public AcceptListener(Context mContext, int mPosition) {
	    			context = mContext;
	    			position = mPosition;
				}

				@Override
	    		public void onClick(View v) {
	        		//Customer ID
	    			//String Result = new String();
	    			//Result = JSON_Seller_Action_Reservation.JSON_Seller_Action_Accept_Reservation(items.get(position).getItemNumRsv());
	    			items.get(position).setItemStatus(DataManager.RESERVATION_STATUS_CONFIRMED);


	    	        reservationRepository.Open();
	    	        reservationRepository.Update(items.get(position));

	    	    	reservationRepository.Close();
	    	    	
	    	    	//Remove from list
	    	    	items.remove(position);
	    			
	    			//Toast.makeText(context, "Accept : "+ Integer.toString(position), Toast.LENGTH_SHORT).show();
	    			Update();
	        	}
	        }

	        class DeclineListener implements View.OnClickListener{
	        	private int position;
	        	private Context context;

	    		public DeclineListener(Context mContext, int mPosition) {
	    			context = mContext;
	    			position = mPosition;
				}

	    		@Override
	    		public void onClick(View v) {
	        		//Customer ID
	    			//String Result = new String();
	    			//Result = JSON_Seller_Action_Reservation.JSON_Seller_Action_Decline_Reservation(items.get(position).getItemNumRsv());
	    			items.get(position).setItemStatus(DataManager.RESERVATION_STATUS_PENDING);

	    	        reservationRepository.Open();
	    	        reservationRepository.Update(items.get(position));

	    	    	reservationRepository.Close();
	    	    	
	    	    	//Remove from list
	    	    	items.remove(position);

	    			//Toast.makeText(context, "Decline : "+ Integer.toString(position), Toast.LENGTH_SHORT).show();

	    			Update();
	        	}
	        }
	}